<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SVGA Preview</title>
    <!-- 引入 SVGAPlayer-Web 的 CDN 链接 -->
    <script src="./svga.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background-color: #f5f5f5;
        }
        #canvas {
            width: 750px;
            height: 1200px;
            background-color: black;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .error {
            color: #f56c6c;
            text-align: center;
            padding: 20px;
        }
        .loading {
            text-align: center;
            padding: 20px;
            color: #909399;
        }
    </style>
</head>
<body>
    <div id="canvas"></div>
    <div id="loading" class="loading">正在加载SVGA文件...</div>
    <div id="error" class="error" style="display: none;"></div>
    <script>
        // 从URL参数中获取SVGA文件链接
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURIComponent(r[2]);
            return null;
        }

        var svgaUrl = getUrlParam('url');
        
        if (!svgaUrl) {
            document.getElementById('loading').style.display = 'none';
            document.getElementById('error').style.display = 'block';
            document.getElementById('error').textContent = '未提供SVGA文件URL参数';
        } else {
            try {
                // 初始化播放器，绑定到页面的一个div元素上
                var player = new SVGA.Player('#canvas');
                // 创建解析器
                var parser = new SVGA.Parser('#canvas');
                
                // 加载并播放指定的SVGA文件
                parser.load(svgaUrl, function(videoItem) {
                    document.getElementById('loading').style.display = 'none';
                    player.setVideoItem(videoItem);
                    player.startAnimation();
                    
                    // 设置循环播放
                    player.loops = 1;
                }, function(error) {
                    document.getElementById('loading').style.display = 'none';
                    document.getElementById('error').style.display = 'block';
                    document.getElementById('error').textContent = '加载SVGA文件失败: ' + (error.message || '未知错误');
                });
            } catch (e) {
                document.getElementById('loading').style.display = 'none';
                document.getElementById('error').style.display = 'block';
                document.getElementById('error').textContent = '初始化失败: ' + e.message;
            }
        }
    </script>
</body>
</html>


